package com.tuanzi.loan.business.repository.collaborate.impl;

import com.tuanzi.loan.business.repository.collaborate.custom.CollaborateChannelUserRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateConfigListResponse;
import org.jooq.Record;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.generated.jooq.loan.Tables.NEW_COLLABORATE_CHANNEL_RATIO;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.NewCollaborateChannelUser.NEW_COLLABORATE_CHANNEL_USER;


/**
 * User: liangZongXi
 * Create time: 14:38 2018/9/18
 * Description:
 */
@Repository
public class CollaborateChannelUserRepositoryImpl extends JOOQBaseRepository implements CollaborateChannelUserRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return NEW_COLLABORATE_CHANNEL_USER;
    }

    @Override
    public Page<CollaborateConfigListResponse> list(CollaborateConfigListRequest searchRequest, Pageable pageable) {
        fields(NEW_COLLABORATE_CHANNEL_USER.fields());
        fields(NEW_COLLABORATE_CHANNEL_RATIO.RATIO, NEW_COLLABORATE_CHANNEL_RATIO.TOMORROW_RATIO, NEW_COLLABORATE_CHANNEL_RATIO.SHOW_TYPE,
                NEW_COLLABORATE_CHANNEL_RATIO.MARKET_SHOW, NEW_COLLABORATE_CHANNEL_RATIO.CHECK_SHOW);
        fields(COMM_PRODUCT_CHANNEL.CHANNEL_NAME);

        leftJoin(NEW_COLLABORATE_CHANNEL_RATIO, NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID.eq(NEW_COLLABORATE_CHANNEL_RATIO.CHANNEL_ID));
        leftJoin(COMM_PRODUCT_CHANNEL, NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID.eq(COMM_PRODUCT_CHANNEL.CHANNEL_CODE));

        Integer[] channels = searchRequest.getChannels();
        if (channels != null && channels.length > 0) {
            List<Integer> collect = Arrays.stream(channels).collect(Collectors.toList());
            in(NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID, collect);
        }

        eq(NEW_COLLABORATE_CHANNEL_USER.USER_NAME, searchRequest.getAccount());

        return search(pageable, record -> {
            CollaborateConfigListResponse response = new CollaborateConfigListResponse();
            response.setId(record.get(NEW_COLLABORATE_CHANNEL_USER.ID));
            response.setAccount(record.get(NEW_COLLABORATE_CHANNEL_USER.USER_NAME));
            response.setChannelId(record.get(NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID));
            response.setChannelName(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setTodayRatio(record.get(NEW_COLLABORATE_CHANNEL_RATIO.RATIO));
            response.setTomorrowRatio(record.get(NEW_COLLABORATE_CHANNEL_RATIO.TOMORROW_RATIO));
            response.setShowType(record.get(NEW_COLLABORATE_CHANNEL_RATIO.SHOW_TYPE));
            response.setProductId(record.get(NEW_COLLABORATE_CHANNEL_USER.PRODUCT_ID));
            response.setMarketShow(record.get(NEW_COLLABORATE_CHANNEL_RATIO.MARKET_SHOW));
            response.setCheckShow(record.get(NEW_COLLABORATE_CHANNEL_RATIO.CHECK_SHOW));
            return response;
        });
    }

    @Override
    public CollaborateConfigListResponse findCollaborateConfigInfo(Integer channelUserId) {
        fields(NEW_COLLABORATE_CHANNEL_USER.fields());
        fields(NEW_COLLABORATE_CHANNEL_RATIO.RATIO, NEW_COLLABORATE_CHANNEL_RATIO.TOMORROW_RATIO, NEW_COLLABORATE_CHANNEL_RATIO.SHOW_TYPE,
                NEW_COLLABORATE_CHANNEL_RATIO.CHECK_SHOW,NEW_COLLABORATE_CHANNEL_RATIO.MARKET_SHOW);
        leftJoin(NEW_COLLABORATE_CHANNEL_RATIO, NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID.eq(NEW_COLLABORATE_CHANNEL_RATIO.CHANNEL_ID));
        eq(NEW_COLLABORATE_CHANNEL_USER.ID, channelUserId);

       // CollaborateConfigListResponse into = searchOne().into(CollaborateConfigListResponse.class);

        Record record = searchOne();

        CollaborateConfigListResponse response = new CollaborateConfigListResponse();

        response.setId(record.get(NEW_COLLABORATE_CHANNEL_USER.ID));
        response.setAccount(record.get(NEW_COLLABORATE_CHANNEL_USER.USER_NAME));
        response.setChannelId(record.get(NEW_COLLABORATE_CHANNEL_USER.CHANNEL_ID));
        response.setTodayRatio(record.get(NEW_COLLABORATE_CHANNEL_RATIO.RATIO));
        response.setTomorrowRatio(record.get(NEW_COLLABORATE_CHANNEL_RATIO.TOMORROW_RATIO));
        response.setShowType(record.get(NEW_COLLABORATE_CHANNEL_RATIO.SHOW_TYPE));
        response.setProductId(record.get(NEW_COLLABORATE_CHANNEL_USER.PRODUCT_ID));
        response.setMarketShow(record.get(NEW_COLLABORATE_CHANNEL_RATIO.MARKET_SHOW));
        response.setCheckShow(record.get(NEW_COLLABORATE_CHANNEL_RATIO.CHECK_SHOW));

        return response;
    }
}
